
#include "rtppollthread.h"

//#ifdef RTP_SUPPORT_THREAD

//#include "rtpsession.h"
//#include "rtcpscheduler.h"
//#include "rtperrors.h"
//#include "rtprawpacket.h"
#include <time.h>

#ifndef _WIN32_WCE
	#include <iostream>
#endif // _WIN32_WCE

//#include "rtpdebug.h"


RTPPollThread::RTPPollThread(SOCKET sock)//:rtpsession(session),rtcpsched(sched)
{
//	stop = false;
//	transmitter = 0;
//#if (defined(WIN32) || defined(_WIN32_WCE))
//	//timeinit.Dummy();
//#endif // WIN32 || _WIN32_WCE
}

RTPPollThread::~RTPPollThread()
{
	Stop();
}
 
int RTPPollThread::Start()
{
	if (JThread::IsRunning())
		return -1;
	if (!stopmutex.IsInitialized())
	{
		if (stopmutex.Init() < 0)
			return -2;
	}
	stop = false;
	if (JThread::Start() < 0)
		return -3;

	return 0;

}

void RTPPollThread::Stop()
{	
	if (!IsRunning())
		return;
	
	stopmutex.Lock();
	stop = true;
	stopmutex.Unlock();
	
	bool done = false;

	if (JThread::IsRunning())
	{
		printf("RTPPollThread: Warning! Having to kill thread!\n");
		JThread::Kill();
	}
	stop = false;
}

void *RTPPollThread::Thread()
{
	JThread::ThreadStarted();
	
	bool stopthread;

	stopmutex.Lock();
	stopthread = stop;
	stopmutex.Unlock();
	while (!stopthread)
	{
		::Sleep(0);
		stopmutex.Lock();
		stopthread = stop;
		stopmutex.Unlock();
	}
	return NULL;
}

//#endif // RTP_SUPPORT_THREAD

